//
//  MQNewFeatureViewController.swift
//  MQWeibo
//
//  Created by 毛强 on 15/12/30.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

class MQNewFeatureViewController: UIViewController, UIScrollViewDelegate  {
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    private func setupUI(){
        
        // 添加视图，以及约束
        view.addSubview(scrollView)
        view.addSubview(pageControl)
        
        scrollView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view)
        }
        
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.centerY.equalTo(view.snp_bottom).offset(-100)
        }
        
        // for 循环添加 imageView
        let count = 4
        
        for i in 0..<count {
            // 初始化 imageView
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i + 1)"))
            imageView.frame = CGRect(x: CGFloat(i) * SCREENW, y: 0, width: SCREENW, height: SCREENH)
            
            scrollView.addSubview(imageView)
            
            if i == count - 1 {
                setupLastPage(imageView)
            }
        }
        // 设置内容大小，可以滚动
        scrollView.contentSize = CGSizeMake(CGFloat(count) * SCREENW, 0)
        // 给 pageControl 设置页数
        pageControl.numberOfPages = count
    }
    
    
    /// 设置最后一页的内容
    ///
    /// - parameter imageView: 最后一页的 imamge
    private func setupLastPage(imageView: UIImageView) {
        // 开启交互
        imageView.userInteractionEnabled = true
        imageView.addSubview(enterButton)
        imageView.addSubview(sharedButton)
        
        // 添加约束
        enterButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(imageView.snp_centerX)
            make.centerY.equalTo(imageView.snp_bottom).offset(-150)
        }
        
        sharedButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(enterButton)
            make.bottom.equalTo(enterButton.snp_top).offset(-20)
        }
    }
    
    // MARK: - 监听方法
    
    @objc private func sharedButtonClick(button: UIButton) {
        button.selected = !button.selected
    }
    
    @objc private func enterButtonClick(){
        // 切换界面
        print("切换界面")
        // 跳转主页
        NSNotificationCenter.defaultCenter().postNotificationName(MQSwitchRootViewControllerNotification, object: nil)
    }
    
    // MARK: - UIScrollViewDelegate
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        print(scrollView.contentOffset.x / scrollView.frame.width)
        let page = Int(scrollView.contentOffset.x / scrollView.frame.width + 0.5)
        // 设置当前选中的页
        pageControl.currentPage = page
    }
    
    // MARK: - 懒加载控件
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.backgroundColor = UIColor.purpleColor()
        // 开启分布
        scrollView.pagingEnabled = true
        // 关闭弹簧效果
        scrollView.bounces = false
        // 隐藏滚动条
        scrollView.showsHorizontalScrollIndicator = false
        // 设置代理
        scrollView.delegate = self
        
        return scrollView
    }()
    
    private lazy var pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        // 设置不同状态的颜色
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        pageControl.pageIndicatorTintColor = UIColor.purpleColor()
        // pageControl.clipsToBounds = false
        return pageControl
    }()
    
    
    // 进入微博的button
    private lazy var enterButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "enterButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setTitle("进入微博", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        return button
    }()
    
    // 分享按钮
    private lazy var sharedButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "sharedButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        // 设置不同状态的图片
        button.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        
        // 设置文字以及文字的属性
        button.setTitle("分享到微博", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        // 调整边距
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, -5)
        return button
    }()

}
